package com.ykw.parking.controller;

import com.ykw.parking.mapper.ParkingMapper;
import com.ykw.parking.mapper.ReserveMapper;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.Parking;
import com.ykw.parking.pojo1.User;
import com.ykw.parking.pojo1.UserParking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
public class MenuController {

    @RequestMapping("/contact")
    public String contact(){
        return "contact";
    }

    @RequestMapping("/selling")
    public String selling(){
        return "selling";
    }
    @RequestMapping("/projects")
    public String projects(){
        return "projects";
    }
    @Autowired
    ReserveMapper reserveMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ParkingMapper parkingMapper;

    @RequestMapping("/reserveList")
    @ResponseBody
    public List reserve(HttpSession session){
        String  username = (String)session.getAttribute("username");
        List parkings=new ArrayList();
        User user = userMapper.queryUserbyname(username);
        if(user!=null){
            String id = user.getId();
            List<String> parkingIds = reserveMapper.queryParkingByUserId(id);//根据用户的id得到预定的物品的id的集合
            Iterator<String> iterator = parkingIds.iterator();//遍历id集合，并根据id得到parking实例，然后返回给List
            while (iterator.hasNext()) {
                String next =  iterator.next();
                Parking parking = parkingMapper.queryByParkingId(next);
                parkings.add(parking);
            }
            return parkings;
        }
        return parkings;
    }
    @RequestMapping(value ="/deleteReserve")
    @ResponseBody
    public String deleteCart(String parkingid, HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        reserveMapper.deleteReserve(parkingid,user.getId());
        parkingMapper.setReserve(parkingid,"0");
        return "1";
    }
    @RequestMapping(value = "/showmsg")
    @ResponseBody
    public User showmsg(HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        return user;
    }

    @RequestMapping(value = "/changeName",method = RequestMethod.POST)
    @ResponseBody
    public String changeName(@RequestParam("name") String name,HttpSession session){
        String  username = (String)session.getAttribute("username");
        System.out.println(name+"======"+username);
        if(username.equals(name)){
            return null;
        }else{
            int i = userMapper.UpdateUserByName(name, userMapper.queryUserbyname(username).getId());
            if(i>0){
                session.setAttribute("username",name);
                return "修改成功！";
            }else{
                return "修改失败！";
            }
        }
    }

    @RequestMapping(value = "/pps")
    @ResponseBody
    public List<UserParking> pps(HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        List<UserParking> pps = parkingMapper.pps(user.getId());
        return pps;
    }
}
